package com.qubuyer.business.home.view

import android.content.Context
import android.view.ViewGroup
import android.widget.ImageView
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.SizeUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.qubuyer.R
import com.qubuyer.business.good.activity.GoodDetailActivity
import com.qubuyer.databinding.HomeItemBannerBinding
import com.qubuyer.repository.entitys.HomeBannerEntity
import com.qubuyer.utils.NavigationUtil
import com.youth.banner.Banner
import com.youth.banner.BannerConfig
import com.youth.banner.Transformer
import com.youth.banner.listener.OnBannerListener
import com.youth.banner.loader.ImageLoader

class BannerItemView(lifecycleOwner: LifecycleOwner, context: Context, parent: ViewGroup, layoutId: Int) : HomeItemView<List<HomeBannerEntity>, HomeItemBannerBinding>(lifecycleOwner,context, parent, layoutId) {

    private var isBannerRunning: Boolean? = null

    init {
        lifecycleOwner.lifecycle.addObserver(object : LifecycleEventObserver {
            override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
                if (event == Lifecycle.Event.ON_DESTROY) {
                    lifecycleOwner.lifecycle.removeObserver(this)
                } else if (event == Lifecycle.Event.ON_PAUSE && isBannerRunning == true) {
                    dataBinding.banner.releaseBanner()
                    isBannerRunning = false
                } else if (event == Lifecycle.Event.ON_RESUME && isBannerRunning == false) {
                    dataBinding.banner.start()
                    isBannerRunning = true
                }
            }
        })
    }

    override fun isTheSameData(origin: List<HomeBannerEntity>, now: List<HomeBannerEntity>): Boolean {
        if (origin.size != now.size) {
            return false
        }
        val size = origin.size
        for (i in 0 until size) {
            if (origin[i] != now[i]) {
                return false
            }
        }
        return true
    }

    override fun bindView(data: List<HomeBannerEntity>) {
        val banner: Banner = dataBinding.banner
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR)
        //设置图片加载器
        banner.setImageLoader(object : ImageLoader() {
            override fun displayImage(context: Context?, path: Any?, imageView: ImageView) {
                val entity = path as HomeBannerEntity
                Glide.with(context!!).load(entity.ad_code_full).apply(RequestOptions.placeholderOf(R.drawable.placeholder).transform(RoundedCorners(SizeUtils.dp2px(5f)))).into(imageView)
            }
        })
        //设置banner动画效果
        banner.setBannerAnimation(Transformer.Default)
        //设置自动轮播，默认为true
        banner.isAutoPlay(true)
        //设置轮播时间
        banner.setDelayTime(2500)
        banner.setOffscreenPageLimit(data.size)
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.CENTER)
        //设置图片集合
        banner.setImages(data)
        banner.setOnBannerListener(OnBannerListener { position ->
            if (null == data[position].goods_id || 0 == data[position].goods_id) {
                return@OnBannerListener
            }
            NavigationUtil.overlay(mContext, GoodDetailActivity::class.java, data[position].goods_id)
        })
        banner.start()
        isBannerRunning = true
    }
}